package com.rexolar.openapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rexolar.openapi.db.entity.PaasPartnerApp;
import com.rexolar.openapi.db.mapper.PartnerAppMapper;
import com.rexolar.openapi.enums.PartnerStatusEnum;
import com.rexolar.openapi.service.IPartneApp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Slf4j
@Service
public class PartnerAppService extends ServiceImpl<PartnerAppMapper, PaasPartnerApp> implements IPartneApp {
    @Autowired
    private PartnerAppMapper partnerAppMapper;

    public String fetchAppSecret(String appKey) {
        LambdaQueryWrapper<PaasPartnerApp> queryWapper = new LambdaQueryWrapper();
        queryWapper.eq(PaasPartnerApp::getPartnerCode, appKey);
        queryWapper.eq(PaasPartnerApp::getStatus, PartnerStatusEnum.ON.getCode());
        List<PaasPartnerApp> list = partnerAppMapper.selectList(queryWapper);
        if(CollectionUtils.isEmpty(list)){
            log.info("fetchAppSecret is empty,  appKey:{}", appKey);
            return null;
        }
        return list.get(0).getPartnerSecret();
    }
}
